package com.jzsm.modules.sender.senderService.impl.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jzsm.modules.sender.pojo.PushChannelAccount;
import com.jzsm.modules.sender.bo.accountConfig.SmsTencentCloudAccountConfigBo;
import com.jzsm.modules.sender.enums.ChannelTypeEnum;
import com.jzsm.modules.sender.senderService.AbstractMessageSender;
import com.jzsm.modules.sender.vo.SmsMessageVo;
import com.jzsm.common.annotation.Channel;
import com.jzsm.common.utils.AssertUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 腾讯云 短信渠道
 */
@Slf4j
@Component
@Channel(ChannelTypeEnum.SMSTencentCloud)
public class SMSTencentCloudMessageSender extends AbstractMessageSender {
    @Override
    public String sendMessage(String message, PushChannelAccount account) {
        return run(message, account);
    }

    public String run(String message, PushChannelAccount account) {
        // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey
        String accountConfig = account.getAccountConfig();
        SmsTencentCloudAccountConfigBo configBo = JSON.parseObject(accountConfig, SmsTencentCloudAccountConfigBo.class);
        checkAccountConfig(configBo);
        Credential cred = new Credential(configBo.getSecretId(), configBo.getSecretKey());

        ClientProfile profile = new ClientProfile();
        profile.setSignMethod(ClientProfile.SIGN_SHA256);
        TypeReference<SmsMessageVo<String[]>> typeReference = new TypeReference<SmsMessageVo<String[]>>() {
        };
        SmsMessageVo<String[]> smsMessageVo = JSON.parseObject(message, typeReference);
        // 选择就近服务器
        SmsClient client = new SmsClient(cred, "");
        SendSmsRequest request = new SendSmsRequest();
        request.setSmsSdkAppid(String.valueOf(configBo.getAppId()));
        request.setSign(configBo.getSign());
        request.setTemplateID(smsMessageVo.getTemplateId());
        request.setPhoneNumberSet(new String[]{smsMessageVo.getPhoneNum()});
        request.setTemplateParamSet(smsMessageVo.getParams());
        log.info("短信发送请求:{}", SendSmsRequest.toJsonString(request));
        SendSmsResponse response = null;
        try {
            response = client.SendSms(request);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            AssertUtils.throwError(e.getMessage());
        }
        SendStatus[] statuses = response.getSendStatusSet();
        log.info("短信发送请求成功,{phoneNum}", smsMessageVo.getPhoneNum());
        String res = SendSmsRequest.toJsonString(response);
        log.info("腾讯云返回:{}", res);
        SendStatus sendStatus = statuses[0];
        String resCode = sendStatus.getCode();
        boolean isSuccess = Objects.isNull(statuses) || statuses.length == 0 || !Objects.equals(resCode, "Ok");
        AssertUtils.isTrue(res, isSuccess);
        return res;
    }

    /**
     * 校验配置
     *
     * @param configBo
     */
    private void checkAccountConfig(SmsTencentCloudAccountConfigBo configBo) {
        AssertUtils.isNotBlank("appId 不允许为空！", configBo.getAppId());
        AssertUtils.isNotBlank("appKey 不允许为空！", configBo.getAppKey());
        AssertUtils.isNotBlank("secretId 不允许为空！", configBo.getSecretId());
        AssertUtils.isNotBlank("secretKey 不允许为空！", configBo.getSecretKey());
        AssertUtils.isNotBlank("sign 不允许为空！", configBo.getSign());
    }
}
